<template>
  <div class="">
    <el-form :inline="true">
      <div style="display: flex; justify-content: space-between;">
        <div />
        <el-form-item>
          <el-button type="primary" @click="onVisible(1)">新增</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center" />
      <el-table-column prop="coinicon" label="币种图标" width="100" align="center">
        <template slot-scope="{row}">
          <el-image style="width: 50px; height: 50px" :src="row.coinicon" :preview-src-list="[row.coinicon]" />
        </template>
      </el-table-column>
      <el-table-column prop="coinname" label="币种名称" align="center" />
      <el-table-column prop="coincode" label="币种编码" align="center" />

      <el-table-column prop="coinType" label="币种类型" width="120" align="center">
        <template slot-scope="{row}">
          <el-tag v-if="row.coinType == 1" type="danger">平台资产</el-tag>
          <el-tag v-if="row.coinType == 2" type="success">区块链</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="ct" label="转账允许" width="80" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.ct == 0" type="danger">不允许</el-tag>
          <el-tag v-else type="success">允许</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="coinNet" label="链类型" width="80" align="center" />
      <el-table-column prop="tfee" label="转账手续费" width="120" align="center" />
      <el-table-column prop="cnyfee" label="cny费率" width="80" align="center" />
      <el-table-column prop="usdtfee" label="usdt费率" width="80" align="center" />
      <el-table-column prop="state" label="状态" width="80" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.state"
            :active-value="1"
            :inactive-value="2"
            @change="(val) => { return SetValid(val, scope.row, 'state') }"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="onVisible(2, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block " style="padding-top: 20px;">
      <el-pagination
        background
        :current-page="PageIndex"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="PageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        @size-change="idx => { pageSize = idx }"
        @current-change="idx => { PageIndex = idx }"
      />
    </div>
    <el-dialog
      :title="visibleType == '1' ? '新增' : '修改'"
      :visible.sync="dialogVisible"
      width="40%"
      :close-on-click-modal="false"
      @closed="handleClose"
    >
      <div>
        <el-form ref="forms" :rules="rules" :model="form" label-width="150px">
          <el-form-item label="币种名称" prop="coinname">
            <el-input v-model="form.coinname" placeholder="请输入币种名称" />
          </el-form-item>
          <el-form-item label="币种编码" prop="coincode">
            <el-input v-model="form.coincode" placeholder="请输入币种编码" />
          </el-form-item>
          <el-form-item label="币种枚举id" prop="coinEnid">
            <el-input v-model="form.coinEnid" placeholder="请输入币种枚举id" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" />
          </el-form-item>
          <el-form-item label="币种类型" prop="coinType">
            <el-select v-model="form.coinType" placeholder="币种类型">
              <el-option label="平台资产" :value="1" />
              <el-option label="区块链" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="cny费率" prop="cnyfee">
            <el-input v-model="form.cnyfee" placeholder="请输入cny费率" />
          </el-form-item>
          <el-form-item label="usdt费率" prop="usdtfee">
            <el-input v-model="form.usdtfee" placeholder="请输入usdt费率" />
          </el-form-item>
          <el-form-item label="链类型" prop="coinNet">
            <el-input v-model="form.coinNet" placeholder="请输入链类型" />
          </el-form-item>
          <el-form-item label="转账手续费" prop="tfee">
            <el-input v-model="form.tfee" placeholder="请输入转账手续费" />
          </el-form-item>
          <div>
            <el-form-item label="图标" prop="headImage">
              <el-upload
                :action="uploadUrl"
                class="avatar-uploader"
                :before-upload="beforeAvatarUpload"
                list-type="picture-card"
                :show-file-list="false"
                :headers="headers"
                :on-success="quillImgSuccess"
                :on-preview="handleSuccess"
                :on-remove="handleRemove"
              >
                <img v-if="headImage" :src="headImage" class="avatar">
                <i v-else class="el-icon-plus" />
              </el-upload>
            </el-form-item>
          </div>
          <el-form-item label="转账允许" prop="ct">
            <el-switch v-model="form.ct" :active-value="1" :inactive-value="0" />
          </el-form-item>
          <el-form-item label="启用/禁用" prop="state">
            <el-switch v-model="form.state" :active-value="1" :inactive-value="0" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onEdit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="dialogImg">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { mapState } from 'vuex'

import { GetSysCoin, EditCoin, SetState } from '@/api/coin.js'
import ipConfig from '../../../../ip-config'
export default {
  name: 'Name',
  data() {
    return {
      key: '',
      type: '', // 币种类型
      dialogImageUrl: '',
      dialogImg: false,
      form: {},
      uploadUrl: ipConfig.baseIP + "/ctrls/Upload?type='img'&file_type=''",
      headers: {
        TOKEN: getToken()
      },

      listLoading: false,
      tableData: [],
      totalCount: 0,
      PageIndex: 1,
      PageSize: 10,

      dialogVisible: false,
      visibleType: '',
      headImage: '', // 修改时显示的图片路径
      rules: {
        coincode: [
          { required: true, message: '请输入币种编码', trigger: 'blur' }
        ],
        coinname: [
          { required: true, message: '请输入币种名称', trigger: 'blur' }
        ],
        coinEnid: [
          { required: true, message: '请输入枚举id', trigger: 'blur' }
        ],
        cnyfee: [
          { required: true, message: '请输入cny费率', trigger: 'blur' }
        ],
        usdtfee: [
          { required: true, message: '请输入usdt费率', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请选择排序', trigger: 'blur' }
        ]
      },
      coiniconArr: []
    }
  },
  computed: {},
  watch: {
    PageIndex(val) {
      console.log(val)
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    setFrom() {
      this.form = {
        id: 0,
        coincode: '', // 编码
        coinname: '', // 名称
        coinicon: '', // 图标
        state: 1, // 状态
        coinType: 0, // 类型
        coinEnid: 0, // 枚举id
        sort: 0, // 排序
        cnyfee: 0, // cny费率
        usdtfee: 0, // usdt费率
        coinNet: 'erc20',
        tfee: 0,
        ct: 0
      }
    },
    // 上传前
    beforeAvatarUpload(file) {
      const fileType = file.type
      if (
        fileType !== 'image/jpeg' &&
        fileType !== 'image/png' &&
        fileType !== 'image/gif' &&
        fileType !== 'image/bmp'
      ) {
        this.$msgbox({
          title: '',
          message: '请插入图片类型文件(jpg/jpeg/png)',
          type: 'warning'
        })
        return false
      }

      // 获取上传文件大小
      const isLt1M = Number(file.size / 1024 / 1024) < 1
      if (!isLt1M) {
        this.$msgbox({
          title: '',
          message: '文件大小不能超过1M，请重新上传。',
          type: 'warning'
        })
        return isLt1M
      }
    },
    // 上传成功
    quillImgSuccess({ success, data }, file) {
      if (success) {
        this.headImage = data
      } else {
        this.$message.error(msg)
      }
    },

    // 删除
    handleRemove({ status, url }, file) {
      this.headImage = ''
    },
    // 预览
    handleSuccess(file) {
      this.dialogImageUrl = file
      this.dialogImg = true
    },
    // 状态
    async SetValid(val, item, type) {
      try {
        console.log(item)
        const confirm = await this.$confirm('确定修改状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm == 'confirm') {
          const { success, msg } = await SetState({
            state: val
          }, [item.id])
          if (success) {
            this.getData()
          }
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        }
      } catch (e) {
        // 取消
        console.log(e)
        this.getData()
      }
    },
    onEdit() {
      this.$refs['forms'].validate(async(valid) => {
        if (valid) {
          this.form.coinicon = this.headImage
          const { success, msg } = await EditCoin(this.form)
          if (success) {
            this.dialogVisible = false
            this.getData()
          }
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        } else {
          return false
        }
      })
    },
    // 关闭前
    handleClose() {
      this.$refs.forms.resetFields()
    },
    // 打开前
    onVisible(type, item = {}) {
      this.visibleType = type
      this.setFrom()
      this.form = { ...this.form, ...item }
      if (type == 2) {
        this.headImage = this.form.coinicon
      }
      console.log(this.form)
      this.dialogVisible = true
    },
    async onSearch() { },
    async getData() {
      this.listLoading = true
      const { list, pageIndex, totalCount } = await GetSysCoin({
        key: this.key,
        cointype: this.type,
        PageIndex: this.PageIndex,
        PageSize: this.PageSize
      })
      this.totalCount = totalCount
      console.log(list)
      this.tableData = list
      this.listLoading = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
<style lang="scss">
.el-table .warning-row {
  background: oldlace;
}

// 上传图片相关样式
.avatar-uploader .el-upload {
  width: 178px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 100%;
  height: 100%;
  display: block;
}
</style>
